package cn.kenny.resource.controller;

import cn.kenny.resource.constant.MessageConstant;
import cn.kenny.resource.entity.PageResult;
import cn.kenny.resource.entity.QueryPageBean;
import cn.kenny.resource.entity.Result;
import cn.kenny.resource.pojo.Doc;
import cn.kenny.resource.pojo.User;
import cn.kenny.resource.pojo.Word;
import cn.kenny.resource.service.AdminService;
import cn.kenny.resource.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("list")
    public Result list(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = adminService.userPageQuery(queryPageBean);
        if(pageResult.getRows() != null && pageResult.getRows().size() > 0){
            return new Result(true, MessageConstant.QUERY_USERLIST_SUCCESS,pageResult);
        }
        return new Result(false, MessageConstant.QUERY_USERLIST_FAIL,pageResult);
    }

    @RequestMapping("filelist")
    public Result fileList(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = adminService.docPageQuery(queryPageBean);
        if(pageResult.getRows() != null && pageResult.getRows().size() > 0){
            return new Result(true, MessageConstant.QUERY_DOCUMENTLIST_SUCCESS,pageResult);
        }
        return new Result(false, MessageConstant.QUERY_DOCUMENTLIST_FAIL,pageResult);
    }

    @RequestMapping("adduser")
    public Result addUser(@RequestBody User user){
        try {
            adminService.addUser(user);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }

    @RequestMapping("edituser")
    public Result editUser(@RequestBody User user){
        try {
            adminService.editUser(user);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
    }

    @RequestMapping("deluser")
    public Result delUser(Integer id){
        Integer num = adminService.deleteById(id);
        if(num > 0)
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        return new Result(false,MessageConstant.DELETE_USER_FAIL);
    }
}
